Sitecore has several scheduled agents that can take care of maintaining the working areas of the databases within the solution.
All clean up tasks have one of the following two structures:
<agent type="Sitecore.ClassName" method="Run" interval="00:00:00" />
Or
<agent type="Sitecore.ClassName,AssemblyName" method="Run" interval="00:00:00" />
Parameters
The following parameters should be defined within the task.
- type - This parameter defines the class of this agent.
- method - This parameter contains the method which will be executed in the class Sitecore.ClassName.
- interval - This parameter describes how often this agent will be executed.
Note: If the parameter interval is set to “00:00:00” this agent will be inactive.
1. Clean up client data
Details
Sitecore has its own session settings which are saved in the SQL database Core in the table ClientData. This task sets how often old and unused records will be removed from the table ClientData.
Format
<agent type="Sitecore.Tasks.CompactClientDataAgent" method="Run" interval="04:00:00" />
2. Clean up history data
Details
Sitecore history data is saved in the History table of the SQL databases. This agent clears history data from all the Sitecore databases. How long history data can be stored in the databases is defined in the following setting in the Core web definition in web.config.
<Engines.HistoryEngine.Storage>
<obj type="Sitecore.Data.$(database).$(database)HistoryStorage, Sitecore.$(database)">
<param desc="connection" ref="connections/$(id)">
</param>
<EntryLifeTime>30.00:00:00</EntryLifeTime>
</obj>
</Engines.HistoryEngine.Storage>
Format
<agent type="Sitecore.Tasks.CleanupHistory" method="Run" interval="04:00:00" />
3. Clean up the publishing queue
Details
The Sitecore publishing queue is saved in the SQL database Core in the PublishQueue table. Sitecore calls CleanupPublishQueue periodically to clear used data from the publishing queue so that it does not grow too large. The publishing queue is simply a list of items that are scheduled for publication on a specific date.
This agent removes all items from the queue with dates up to and including the date given in the DaysToKeep parameter.
For more information about the publishing queue see the following link: sdn5/Developer/Integrating External Data Sources/Implementation/Publishing Support
Format
<agent type="Sitecore.Tasks.CleanupPublishQueue" method="Run" interval="00:02:00">
<DaysToKeep>30</DaysToKeep>
</agent>
4. Clean up the HTML cache
Details
This agent clears the HTML cache from all the web sites defined in web.config.
Format
<agent type="Sitecore.Tasks.HtmlCacheClearAgent" method="Run" interval="00:00:00" />
5. Clean up work files
Details
This agent deletes any physical files from the specified folders which are defined in the section <remove>.
If the variable <sc.variable name="dataFolder" value="/data" />is defined in the <sitecore database="SqlServer"> section in your web.config then the cleanup agent will remove old files in the following folders:
Data/logs, Data/audit, Data/viewstate and Data/MediaCache.
In the section remove you can declare the folders which the cleanup agent will check.
These sections define the folders and files which will be removed.
The remove section has the following parameters;
- folder – define the folder where the cleanup agent will check and delete old or unnecessary files. The default value is $(dataFolder).
- pattern - define the regular expression for the name of the files which will be checked by the cleanup agent. If you set pattern=”” then this folder will not check anything.
- recursive – define whether to check subfolders. The default value is true.
- mode – if you set mode=”off” then this folder will not be checked otherwise the cleanup agent will check the defined folder.
- minAge – defines the minimum age of the file. If this time is prior to the time of creation or last modification for this file then this file will not be deleted. The default value is 30 minutes.
- maxAge – defines the maximum age of the file. If this time is prior to the time of creation or last modification for this file then this file will be deleted.
- minCount – defines the minimum count of files. The default value is 0.
- maxCount - defines maximum count of files. If the count of files is more than this parameter then the oldest files are deleted. The files cannot be deleted if their age is less than the parameter minAge.
- rolling - if set to rolling="true", minCount and maxCount will be ignored. The default value is false.
- strategy - number of files to process within hour, day, week, month, year.
The parameter LogActivity defines whether or not activity messages will be output to the log file.
Format
<agent type="Sitecore.Tasks.CleanupAgent" method="Run" interval="06:00:00">
<!-- Specifies files to be cleaned up.
If rolling="true", [minCount] and [maxCount] will be ignored.
[minAge] and [maxAge] must be specified as [days.]hh:mm:ss.
The default value of [minAge] is 30 minutes.
[strategy]: number of files within hour, day, week, month, year
[recursive=true|false]: check subfolders?
-->
<files hint="raw:AddCommand">
<remove folder="$(dataFolder)/audit" pattern="audit.*.txt" maxCount="5" minAge="01:00:00" rolling=”true”/>
<remove folder="$(dataFolder)/logs" pattern="log.*.txt" minCount="2" minAge="01:00:00" strategy=”4”/>
<remove folder="$(dataFolder)/viewstate" pattern="*.txt" maxAge="01:00:00" recursive="true" mode=”off”/>
<remove folder="/App_Data/MediaCache" pattern="*.*" maxAge="90.00:00:00" recursive="true" />
</files>
<LogActivity>true</LogActivity>
</agent>
6. Sending agents
Sending agents
The agents support sending parameters to the class’s constructor, fields and methods in exactly the same manner as other objects managed by the system such as event handlers and pipeline processors:
Send parameters to the class’s constructor.
To add a parameter to the class’s constructor you should add this section to your agent:
<param desc="Description"> Value </param>
For example:
Add in web.config.
<agent type="SendParametersToAgent.CustomAgent" method="Run" interval="00:00:10">
<param desc="First integer parameter in the constructor"> 100
</param>
<param desc="Second string parameter in the constructor" >Sitecore</param>
</agent>
Sample code for the constructor.
public CustomAgent(string param1, string param2)
{
Assert.ArgumentNotNullOrEmpty(param1, "First parameter");
Assert.ArgumentNotNullOrEmpty(param2, "Second parameter");
int IntParam = int.Parse(param1);
String StrParam = param2;
}
Initializating class’s fields.
To initialize the class’s fields you can add the next section to your agent.
<Name of the property> Value </Name of the property>
For example:
Add in web.config.
<agent type="SendParametersToAgent.CustomAgent" method="Run" interval="00:00:10">
<Count>50</Count>
</agent>
Sample code for the field and properties.
public class CustomAgent
{
// Fields
private int my_count;
//Properties
public int Count
{
set
{
this.my_count = value;
}
get
{
return this.my_count;
}
}
}
Call class’s method with type’s parameter System.Xml.XmlNode.
To call the class’s method
public void CustomMethod(System.Xml.XmlNode configNode){}
you should add the following sections to your agent.
<Name hint="raw:CustomMethod">
<LocalName value1="value" value2="value" … valueN="value"/>
<LocalName value1="value" value2="value" … valueN="value"/>
…
<LocalName value1="value" value2="value" … valueN="value"/>
</Name>
For example:
Add in web.config.
<agent type="SendParametersToAgent.CustomAgent" method="Run" interval="00:00:10">
< field hint="raw:CustomMethod">
<min minCount="15" minAge="01:00:00" />
</field>
</agent>
Sample code for the function CustomMethod(System.Xml.XmlNode configNode){}.
public void SetMinValues(XmlNode configNode)
{
NameValueCollection my_configSettings = Sitecore.Xml.XmlUtil.GetAttributes(configNode);
int minCount = MainUtil.GetInt(my_configSettings["minCount"], 0);
TimeSpan minAge = DateUtil.ParseTimeSpan(my_configSettings["minAge"], new TimeSpan(0, 30, 0));
}
7. Examples of Implementation
The code associated with this article provides a sample implementation of a task with custom parameters.
To see how to create the scheduled task, look at the following link:
http://sdn.sitecore.net/SDN5/FAQ/Administration/Scheduled tasks.html
Open web.config and copy to the <scheduling> section the following agent :
<scheduling>
<frequency>00:10:00</frequency>
<agent type="SendParametersToAgent.CustomAgent" method="Run" interval="00:05:00">
<param desc="First integer parameter in the constructor"> 100
</param>
<param desc="Second string parameter in the constructor">
Sitecore</param>
<Count>50</Count>
<MyArray hint="list">
<value>one</value>
<value>two</value>
<value>three</value>
</MyArray>
<range hint="raw:CustomMethod">
<min minCount="15" minAge="01:00:00" />
<max maxCount="40" maxAge="10:00:00" />
</range>
</agent>
</scheduling>